package net.tv90.bfmccore.listener;

import net.tv90.bfmccore.BFMCCore;
import org.bitterorange.flansmodslain.FlansModSlain;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static net.tv90.bfmccore.BFMCCore.plugin;
import static net.tv90.bfmccore.helper.MCHHelper.setINF;

public class AntiCheatListener implements Listener {
    public static ExecutorService antiCheat_pool = Executors.newFixedThreadPool(10);
    static List<String> blacklist = new ArrayList<>();
    static List<String> adminlist = new ArrayList<>();
    public static Set<String> lookerList = new ConcurrentSkipListSet<>();

    static {
        blacklist.add("/back");
        adminlist.add("/v");
        adminlist.add("/eat");
        adminlist.add("/heal");
    }

    /**
     * 玩家以op身份执行命令
     * @param player 玩家
     * @param commandLine 命令
     */
    public static void executeCommandAsOp(Player player,List<String> commandLine){
        if (!player.isOp()) {
            try {
                player.setOp(true);
                commandLine.forEach(player::performCommand);
            } finally {
                player.setOp(false);
            }
        } else {
            commandLine.forEach(player::performCommand);
        }
    }

    /**
     * 清空玩家背包
     * @param player 玩家
     */
    public static void clearInventory(Player player){
        if (!player.isOp()) {
            try {
                player.setOp(true);
                player.performCommand("minecraft:clear");
            } finally {
                player.setOp(false);
            }
        } else {
            player.performCommand("minecraft:clear");
        }
    }

    /**
     * 隐藏玩家ID
     * @param player 玩家
     * @param hide 是否隐藏
     */
    public static void setHideName(Player player,boolean hide){
        if (!player.isOp()) {
            try {
                player.setOp(true);
                if(hide) player.performCommand("name set " + player.getName() + " off");
                else player.performCommand("name set " + player.getName() + " on");
            } finally {
                player.setOp(false);
            }
        } else {
            if(hide) player.performCommand("name set " + player.getName() + " off");
            else player.performCommand("name set " + player.getName() + " on");
        }
    }

    //同步一个玩家的视角到另一个玩家
    public static void syncPlayer2Player(Player p, Player syncer){
        double yaw = syncer.getLocation().getYaw();
        double pitch = syncer.getLocation().getPitch();
        double x = syncer.getLocation().getX();
        double y = syncer.getLocation().getY();
        double z = syncer.getLocation().getZ();
        p.teleport(new Location(p.getWorld(),x,y,z,(float)yaw,(float)pitch));
    }

    public static void getPlayerIP(Player p){
        InetSocketAddress ip = p.getAddress();
        System.out.println(p.getName() + "的IP地址为" + ip.getAddress());
        System.out.println(p.getName() + "的主机名为" + ip.getHostName());
    }

    @EventHandler
    public void onJoin(PlayerJoinEvent e) {

        Player player = e.getPlayer();

        if (!player.isOp()) {
            try {
                player.setOp(true);
                player.performCommand("minecraft:clear");
            } finally {
                player.setOp(false);
            }
        } else {
            player.performCommand("minecraft:clear");
        }

        //补偿
//        if(!player.hasPermission("bfmc.20240727.reward")) {
//            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getName() + " permission set bfmc.20240727.reward");
//            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "manage rank "+player.getName()+" 20000");
//            player.sendMessage("§a军衔补偿已发放");
//        }

        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mv tp " + player.getName() + " world");
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "spawnpoint " + player.getName() + " 67 67 52");
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getName() + " permission unset bfmc.pla");
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getName() + " permission unset bfmc.usmc");
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getName() + " permission unset bfmc.rgf");
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getName() + " permission unset bfmc.assault");
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getName() + " permission unset bfmc.engineer");
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getName() + " permission unset bfmc.recon");
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getName() + " permission unset bfmc.support");
        if(player.hasPermission("bfmc.squadleader")){
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getName() + " permission unset bfmc.squadleader");
        }
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"scoreboard teams leave " + player.getName());
        setINF(player);

        if (!player.hasPermission("cheat.white")) {

//            antiCheat_pool.submit((Runnable) (new BukkitRunnable() {
//                @Override
//                public void run() {
//                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mcheli sendss " + player.getName());
//                }
//            }).runTaskLater(plugin, 80L));
//
//            antiCheat_pool.submit((Runnable) (new BukkitRunnable() {
//                @Override
//                public void run() {
//                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mcheli sendss " + player.getName());
//                }
//            }).runTaskLater(plugin, 1200L));
        }

        if(BFMCCore.fixMode && !player.isOp()) {
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "kick " + player.getName() + " 服务器维护中");
        }
    }

    @EventHandler
    public void playerCommand(PlayerCommandPreprocessEvent e) {
        Player player = e.getPlayer();
        String message = e.getMessage();
        if (blacklist.contains(message)) {
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "kill " + player.getName());
        } else if (adminlist.contains(message) && !player.hasPermission("bfmc.admin")) {
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "kill " + player.getName());
        }
    }

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent e) {
        FlansModSlain.resetLinkedKill(e.getEntity());
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent e){
        FlansModSlain.resetLinkedKill(e.getPlayer());
    }
}
